<?php
/**
 * Created by PhpStorm.
 * User: hhlov_000
 * Date: 15-1-28
 * Time: 下午6:25
 */

namespace Admin\Controller;


class DataController extends CommonController {

     public function userRegister(){
         $menu_db = D('Menu');
         $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
         $this->assign(currentpos, $currentpos);

         $this->display('user_register');
     }
    public function userRegisterData(){

        $user_db = M('user');
        $total_user = $user_db->count();

        $today = date('Y-m-d',time()).' 00:00:00';
        $month_ago = strtotime('-1 month', time());
        $month_ago_day = date('Y-m-d',$month_ago).' 00:00:00';
        $today_register = $user_db->join('user_ext on user.user_id = user_ext.user_id','LEFT')
            ->where("user_ext.regis_time >= '$today'")->count();

        $recent_month = $user_db->join('user_ext on user.user_id = user_ext.user_id','LEFT')
            ->where("user_ext.regis_time >= '$month_ago_day'")->count();

        $today_active = $user_db->join('user_status on user.user_id = user_status.user_id','LEFT')
            ->field(array('user_status.user_id as id'))
            ->where("user_status.login_time >= '$today'")->count('distinct(user_status.user_id)');


        //dump($month_ago_day);
        $this->assign('total_user',$total_user);
        $this->assign('today_register',$today_register);
        $this->assign('recent_month',$recent_month);
        $this->assign('today_active',$today_active);


        $this->display('user_register_data');
    }
    public function userActive(){
        $start = $_POST['start'];
        $end = $_POST['end'];
        if($start == null || $end ==null || $start == '' || $end ==''){
            $end = date("Y-m-d",time());
            $start =date("Y-m-d",time()-86400*30);
        }
        $days = array();
        for($i = strtotime($start); $i <= strtotime($end); $i += 86400)
        {
            $days[]=date("Y-m-d",$i);
        }

        $data1['name'] = '日活跃用户';
        $data1['data'] = array();
        $data1['type'] = 'line';

        $data2['name'] = '日新增用户';
        $data2['data'] = array();
        $data2['type'] = 'line';

        $user_db = M('user');

        for($j=0;$j<sizeof($days);$j++){
            $map_regis['regis_time'] = array('like',$days[$j].'%');
            $map_login['login_time'] = array('like',$days[$j].'%');
            $data2['data'][] = $user_db->join('user_ext on user.user_id = user_ext.user_id','LEFT')
                ->where($map_regis)->count();
            $data1['data'][] = $user_db->distinct(true)->join('user_status on user.user_id = user_status.user_id','LEFT')
                ->field(array('user_status.user_id'))
                ->where($map_login)->count('distinct(user_status.user_id)');
        }

        $result['xdata']=$days;
        $result['legend']=['日活跃用户','日新增用户'];
        $result['series'][]=$data1;
        $result['series'][]=$data2;

        echo json_encode($result);
    }

    public function missionData(){
        $menu_db = D('Menu');
        $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
        $this->assign(currentpos, $currentpos);

        $this->display('mission_data');
    }

    public function missionDataView(){
        $mission_db = M('mission');

        $total_mission = $mission_db->count();

        $map_finish['status'] = array('eq','2');
        $total_finish = $mission_db->where($map_finish)->count();

        $today = date("Y-m-d",time());
        $map_today['first_publish_time'] = array('like',$today.'%');

        $today_publish = $mission_db->where($map_today)->count();

        $map_today_finish['publish_time'] = array('like',$today.'%');
        $map_today_finish['status'] = array('eq','2');

        $today_finish = $mission_db->where($map_today_finish)->count();

        $this->assign('total_mission',$total_mission);
        $this->assign('total_finish',$total_finish);
        $this->assign('today_publish',$today_publish);
        $this->assign('today_finish',$today_finish);

        $this->display('mission_data_view');

    }

    public function missionActive(){
        $start = $_POST['start'];
        $end = $_POST['end'];
        if($start == null || $end ==null || $start == '' || $end ==''){
            $end = date("Y-m-d",time());
            $start =date("Y-m-d",time()-86400*30);
        }
        $days = array();
        for($i = strtotime($start); $i <= strtotime($end); $i += 86400)
        {
            $days[]=date("Y-m-d",$i);
        }

        $data1['name'] = '日新增的活';
        $data1['data'] = array();
        $data1['type'] = 'line';

        $data2['name'] = '日成交的活';
        $data2['data'] = array();
        $data2['type'] = 'line';

        $mission_db = M('mission');

        for($j=0;$j<sizeof($days);$j++){
            $map_publish['first_publish_time'] = array('like',$days[$j].'%');
            //$map_publish['status'] = array('eq','1');
            $map_finish['publish_time'] = array('like',$days[$j].'%');
            $map_finish['status'] = array('eq','2');
            $data2['data'][] = $mission_db->where($map_finish)->count();
            $data1['data'][] = $mission_db->where($map_publish)->count();
        }

        $result['xdata']=$days;
        $result['legend']=['日新增的活','日成交的活'];
        $result['series'][]=$data1;
        $result['series'][]=$data2;

        echo json_encode($result);

    }

    public function operateData(){
        $menu_db = D('Menu');
        $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
        $this->assign(currentpos, $currentpos);

        $this->display('operate_data');
    }

    public function operateDataView(){


        $this->display('operate_data_view');

    }


}